package com.example.administrator.mysaft;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017/6/22.
 */

public class ProcessManagerAdapter extends BaseAdapter{
    private Context context;
    private List<TaskInfo> mUsertaskInfo;
    private List<TaskInfo> mSystaskInfo;
    private SharedPreferences msp;
    public ProcessManagerAdapter(Context context,List<TaskInfo> userTaskInfos,List<TaskInfo> systaskInfo){
        super();
        this.context=context;
        this.mUsertaskInfo=userTaskInfos;
        this.mSystaskInfo=systaskInfo;
        msp=context.getSharedPreferences("config",Context.MODE_PRIVATE);

    }
    public int getCount(){
        if (mSystaskInfo.size()>0 & msp.getBoolean("showSystemProcess",true)){
            return mUsertaskInfo.size()+mSystaskInfo.size()+2;
        }
        else{
            return mUsertaskInfo.size()+1;
        }
    }



    public Object getItem(int position){
        if (position==0||position==mUsertaskInfo.size()+1){
            return null;
        } else if (position<=mUsertaskInfo.size()){
//            用户进程
            return mUsertaskInfo.get(position-1);
        }
        else {
//            系统进程
            return mSystaskInfo.get(position-mUsertaskInfo.size()-2);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View converView, ViewGroup parent){
        if (position==0){
            TextView tv=getTextView();
            tv.setText("用户进程："+mUsertaskInfo.size()+"个");
            return tv;
        }else if (position==mUsertaskInfo.size()+1){
            TextView tv=getTextView();
            if (mSystaskInfo.size()>0){
                tv.setText("系统进程："+mSystaskInfo.size()+"个");
                return tv;
            }
        }
//        获取TaskInfo对象
        TaskInfo taskInfo=null;
        if (position<=mUsertaskInfo.size()){
            taskInfo=mUsertaskInfo.get(position-1);
        }else if (mSystaskInfo.size()>0){
            taskInfo=mSystaskInfo.get(position-mUsertaskInfo.size()-2);
        }
        ViewHolder holder=null;
        if (converView!=null && converView instanceof RelativeLayout){
            holder=(ViewHolder)converView.getTag();
        }else{
            converView=View.inflate(context,R.layout.item_processmanager_list,null);
            holder=new ViewHolder();
            holder.mAppIconImgv= (ImageView) converView.findViewById(R.id.imgv_appicon_processmana);
            holder.mAppMemoryTV= (TextView) converView.findViewById(R.id.tv_appmemory_processmana);
            holder.mAppNameTV=(TextView) converView.findViewById(R.id.tv_appname_processmana);
            holder.mCheckBox= (CheckBox) converView.findViewById(R.id.checkbox);
            converView.setTag(holder);
        }
        if (taskInfo!=null){
            holder.mAppNameTV.setText(taskInfo.appName);
            holder.mAppMemoryTV.setText("占用内存："+ Formatter.formatFileSize(context,taskInfo.appMemory));
            holder.mAppIconImgv.setImageDrawable(taskInfo.appIcon);
            if (taskInfo.packageName.equals(context.getPackageName())){
                holder.mCheckBox.setVisibility(View.GONE);
            }else {
                holder.mCheckBox.setVisibility(View.VISIBLE);
            }
            holder.mCheckBox.setChecked(taskInfo.isChecked);
        }
       return converView;
    }
    private TextView getTextView(){
        TextView tv=new TextView(context);
        tv.setBackgroundColor(context.getResources().getColor(R.color.light_green));
//      tv.setPadding(DensityUtil);
       return tv;
    }
    static class ViewHolder{
        ImageView mAppIconImgv;
        TextView mAppNameTV;
        TextView mAppMemoryTV;
        CheckBox mCheckBox;
    }
}
